package com.sgb.contract.view.widget.pop;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;

import com.swgk.core.dialog.BasePopWindow;

/**
 * 在线合同弹窗自定义
 * @author cyj
 * @param <D>
 */
public abstract class ContractPopWindow<D> extends BasePopWindow<D> {

    public ContractPopWindow(Context context) {
        super(context);
    }

    public ContractPopWindow(Context context, int dir) {
        super(context, dir);
    }

    public ContractPopWindow(Context context, AttributeSet attrs, int dir) {
        super(context, attrs, dir);
    }

    public ContractPopWindow(Context context, AttributeSet attrs, int defStyleAttr, int dir) {
        super(context, attrs, defStyleAttr, dir);
    }

    public ContractPopWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, int dir) {
        super(context, attrs, defStyleAttr, defStyleRes, dir);
    }

    private Handler handler = new Handler(Looper.getMainLooper());

    /**
     * 显示弹窗
     * 弹窗高度算法：父容器的高度 - 参照view的y轴位置(参照view的起始点y点) = 弹窗高度值
     * @param view 参照view
     * @param x    x轴+-向偏移量
     * @param y    y轴+-向偏移量，特别注意y轴的参照view若是有真实高度，则需要设置此值
     */
    @Override
    public void showPopwindow(View view, int x, int y) {
        if (!this.isShowing()) {
            handler.postDelayed(() -> showAsDropDown(view, x, y), 100);
        } else {
            dissMiss();
        }
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        int parentHeight = ((View) anchor.getParent()).getHeight();
        int h = parentHeight - anchor.getTop() - xoff - yoff;
        setHeight(h);
        super.showAsDropDown(anchor, xoff, yoff);
    }
}
